/**
 * Created by aa on 2016/5/13.
 */
$(function () {
    var domOperate = new DomOperation();
    if (domOperate.isOver()) {
        location.href = 'activityend.html';
    }
    domOperate.bindEvent();
    domOperate.setDate();
});

function DomOperation() {
    var dataOperate = new DataOperation();

    //判断活动时候过期
    this.isOver = function () {
        var now = new Date();
        var endDate = dataOperate.getActivityInfo().end;
        endDate = new Date(Date.parse(endDate));
        if (now > endDate) {
            return true;
        }
        else {
            return false;
        }
    };
    this.setDate = function () {
        var dateStr = dataOperate.getActivityInfo().date;
        $('.date').html(dateStr);
    };

    //获取积分
    /*  this.getScore=function(){
     var score = dataOperate.getScore();
     return score;
     };*/
    //绑定事件
    this.bindEvent = function () {
        var self = this;
        $('.titleBack .disCount').on('click', function (event) {
            var buttonId = $(event.target || event.srcElement).attr('id');
            if ($('#' + buttonId).attr('disabled') != 'disabled') {
                //获取积分
                dataOperate.getScore(function (result) {
                    if (buttonId == "bigDisCount") {
                        if (result >= 160) {
                            dataOperate.reducePoints(1, function (leftPoints) {
                                layer.open({
                                    content: '领取加息券成功',
                                    btn: ['OK']
                                });
                                self.setButton(leftPoints);
                            }, function (errorMsg) {
                                layer.open({
                                    content: errorMsg,
                                    btn: ['OK']
                                });
                            });
                        }
                    }
                    else {
                        if (buttonId == "smallDiscount" || buttonId == "disCountButton") {
                            if (result >= 100) {
                                dataOperate.reducePoints(2, function (leftPoints) {
                                    layer.open({
                                        content: '领取加息券成功',
                                        btn: ['OK']
                                    });
                                    self.setButton(leftPoints);
                                }, function (errorMsg) {
                                    layer.open({
                                        content: errorMsg,
                                        btn: ['OK']
                                    });
                                });
                            }
                        }
                    }
                });
            }

            // var score=self.getScore();

            /*self.setButton(score);*/
        });
    };
    //根据积分决定是否将按钮置灰
    this.setButton = function (score) {
        if (score >= 100) return;
        //小于8根据规则屏蔽按钮
        /*  if (score >= 100) {
         var grayOperation = new GrayImage('bigDisCount');
         grayOperation.setGray();
         $('#bigDisCount').attr('disabled','disabled');
         }
         else {*/
        /*   var grayOperation = new GrayImage('bigDisCount');
         grayOperation.setGray();*/
        grayOperation = new GrayImage('smallDiscount');
        grayOperation.setGray();
        $('#bigDisCount').attr('disabled', 'disabled');
        $('#smallDiscount').attr('disabled', 'disabled');
        $('#disCountButton').attr('disabled', 'disabled');
        $('#disCountButton').css('')
        //}
    };
}

window.onload = function () {
    var domOperate = new DomOperation();
    var dataOperate = new DataOperation();
    var score = dataOperate.getScore(function (result) {
        domOperate.setButton(result);
    });

};

